/*
 * 文件名：EmailSenderProxy.java
 * 版权：Copyright by http://www.infopub.top/
 * 描述：
 * 修改人：Awoke
 * 修改时间：2018年11月7日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgc.core.sender.email;


import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.Callable;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import top.infopub.concurrent.ExecutorManager;
import top.infopub.mgc.core.service.MsgTaskService;
import top.infopub.model.mgc.MsgTask;
import top.infopub.vo.ResponseVo;


/**
 * 邮件发送代理
 * @author Awoke
 * @version 2018年11月7日
 * @see EmailSenderProxy
 * @since
 */
@Service
public class EmailSenderProxy {

    private static final Logger logger = LoggerFactory.getLogger(EmailSenderProxy.class);

    @Autowired
    private MsgTaskService msgTaskService;

    @Resource
    private EmailSender emailSender;

    /**
     * 发送邮件
     * @param msgTask 
     * @see
     */
    @SuppressWarnings("unchecked")
    public void sendMail(final MsgTask msgTask) {
        // 消息状态 1：待发送 2：正在发送 3：发送成功 4：发送失败  //
        MsgTask uptMsgTask = new MsgTask();
        uptMsgTask.setId(msgTask.getId());

        // 正在发送
        uptMsgTask.setState(2);
        uptMsgTask.setBeginSendDatetime(new Date());
        uptMsgTask.setUpdatetime(new Date());
        msgTaskService.updateBySelective(uptMsgTask);

        ResponseVo<String> responseVo = new ResponseVo<String>(false, 9999, null);
        // 等待线程返回处理结果
        try {
            responseVo = (ResponseVo<String>)ExecutorManager.getInstance().futureTask(
                new Callable<Object>() {
                    @Override
                    public Object call()
                        throws Exception {
                        // 发送邮件
                        return emailSender.sendMail(msgTask);
                    }
                }, msgTask.getTimeout().multiply(new BigDecimal("60")).longValue());// 转成单位秒
        }
        catch (Exception e) {
            logger.error("", e);
            // 发送失败
            uptMsgTask.setState(4);
            uptMsgTask.setBeginSendDatetime(null);
            uptMsgTask.setUpdatetime(new Date());
            // 异常信息 
            uptMsgTask.setMsgDesc(e.getClass().getName() + " " + e.getMessage());
            msgTaskService.updateBySelective(uptMsgTask);
            // 直接return 防止后续update
            return;
        }
        if (responseVo.isSuccess()) {
            // 发送成功
            uptMsgTask.setState(3);
            uptMsgTask.setBeginSendDatetime(null);
            uptMsgTask.setUpdatetime(new Date());
            msgTaskService.updateBySelective(uptMsgTask);
        }
        else {
            // 发送失败
            uptMsgTask.setState(4);
            uptMsgTask.setBeginSendDatetime(null);
            uptMsgTask.setUpdatetime(new Date());
            // 异常信息 
            uptMsgTask.setMsgDesc(responseVo.getMsg());
            msgTaskService.updateBySelective(uptMsgTask);
        }
    }

}
